home *** CD-ROM | disk | FTP | other *** search
/ Dr. Windows 3 / dr win3.zip / dr win3 / PROGRAMR / OLE2BOOK.ZIP / CHAP13.ZIP / PATRON / PAGEWIN.CPP < prev    next >
C/C++ Source or Header  |  1993-06-27  |  23KB  |  838 lines

  1. /*
  2.  * PAGEWIN.CPP
  3.  * Modifications for Chapter 13
  4.  *
  5.  * Window procedure for the Pages window and support functions.  This
  6.  * window manages its own scrollbars and viewport and provides
  7.  * printing capabilities as well.  The public CPages::Print lives here.
  8.  *
  9.  * Copyright (c)1993 Microsoft Corporation, All Rights Reserved
  10.  *
  11.  * Kraig Brockschmidt, Software Design Engineer
  12.  * Microsoft Systems Developer Relations
  13.  *
  14.  * Internet  :  kraigb@microsoft.com
  15.  * Compuserve:  >INTERNET:kraigb@microsoft.com
  16.  */
  17.  
  18.  
  19.  
  20. #include "patron.h"
  21.  
  22.  
  23. extern HWND g_hDlgPrint;
  24. extern BOOL g_fCancelPrint;
  25.  
  26.  
  27. /*
  28.  * PagesWndProc
  29.  *
  30.  * Purpose:
  31.  *  Window procedure for the Pages window.
  32.  */
  33.  
  34. LRESULT __export FAR PASCAL PagesWndProc(HWND hWnd, UINT iMsg
  35.     , WPARAM wParam, LPARAM lParam)
  36.     {
  37.     LPCPages        ppg;
  38.     PAINTSTRUCT     ps;
  39.     HDC             hDC;
  40.     int             iPos, iTmp;
  41.     int             iMin, iMax;
  42.     UINT            idScroll;
  43.     BOOL            fDirty=FALSE;
  44.  
  45.     ppg=(LPCPages)GetWindowLong(hWnd, PAGEWL_STRUCTURE);
  46.  
  47.     switch (iMsg)
  48.         {
  49.         case WM_CREATE:
  50.             ppg=(LPCPages)((LPCREATESTRUCT)lParam)->lpCreateParams;
  51.             SetWindowLong(hWnd, PAGEWL_STRUCTURE, (LONG)ppg);
  52.  
  53.             ppg->m_hWnd=hWnd;
  54.             break;
  55.  
  56.  
  57.         case WM_PAINT:
  58.             /*
  59.              * If there is currently a drag-rectangle showing, then
  60.              * remove it before painting.  This insures that painting
  61.              * doesn't blast part of that rectangle away such that when
  62.              * we draw it next, garbage is left around.
  63.              */
  64.             if (ppg->m_fDragRectShown)
  65.                 ppg->DrawDropTargetRect(NULL, NULL);
  66.  
  67.             hDC=BeginPaint(hWnd, &ps);
  68.  
  69.             //Draw only if we have a page to show.
  70.             if (0!=ppg->m_cPages)
  71.                 ppg->Draw(hDC, FALSE, FALSE);
  72.  
  73.             EndPaint(hWnd, &ps);
  74.  
  75.             //Turn the rectangle back on, if necessary.
  76.             if (ppg->m_fDragRectShown)
  77.                 ppg->DrawDropTargetRect(NULL, NULL);
  78.  
  79.             break;
  80.  
  81.  
  82.         case WM_HSCROLL:
  83.         case WM_VSCROLL:
  84.             idScroll=(WM_HSCROLL==iMsg) ? SB_HORZ : SB_VERT;
  85.  
  86.             iPos=GetScrollPos(hWnd, idScroll);
  87.             iTmp=iPos;
  88.             GetScrollRange(hWnd, idScroll, &iMin, &iMax);
  89.  
  90.             switch (wParam)
  91.                 {
  92.                 case SB_LINEUP:     iPos -= 20;  break;
  93.                 case SB_PAGEUP:     iPos -=100;  break;
  94.                 case SB_LINEDOWN:   iPos += 20;  break;
  95.                 case SB_PAGEDOWN:   iPos +=100;  break;
  96.  
  97.                 case SB_THUMBPOSITION:
  98.                     iPos=ScrollThumbPosition(wParam, lParam);
  99.                     break;
  100.  
  101.                 //We don't want scrolling on this message.
  102.                 case SB_THUMBTRACK:
  103.                     return 0L;
  104.                 }
  105.  
  106.             iPos=max(iMin, min(iPos, iMax));
  107.  
  108.             if (iPos!=iTmp)
  109.                 {
  110.                 //Set the new position and scroll the window as necessary.
  111.                 SetScrollPos(hWnd, idScroll, iPos, TRUE);
  112.  
  113.                 if (SB_HORZ==idScroll)
  114.                     {
  115.                     ppg->m_xPos=iPos;
  116.                     ScrollWindow(hWnd, iTmp-iPos, 0, NULL, NULL);
  117.                     }
  118.                 else
  119.                     {
  120.                     ppg->m_yPos=iPos;
  121.                     ScrollWindow(hWnd, 0, iTmp-iPos, NULL, NULL);
  122.                     }
  123.                 }
  124.  
  125.             break;
  126.  
  127.         case WM_RBUTTONDOWN:
  128.             if (NULL==ppg->m_pPageCur)
  129.                 break;
  130.  
  131.             fDirty=ppg->m_pPageCur->OnRightDown(wParam
  132.                 , LOWORD(lParam), HIWORD(lParam));
  133.             break;
  134.  
  135.         case WM_LBUTTONDOWN:
  136.             if (NULL==ppg->m_pPageCur)
  137.                 break;
  138.  
  139.             fDirty=ppg->m_pPageCur->OnLeftDown(wParam
  140.                 , LOWORD(lParam), HIWORD(lParam));
  141.             break;
  142.  
  143.         case WM_LBUTTONUP:
  144.             if (NULL==ppg->m_pPageCur)
  145.                 break;
  146.  
  147.             fDirty=ppg->m_pPageCur->OnLeftUp(wParam
  148.                 , LOWORD(lParam), HIWORD(lParam));
  149.             break;
  150.  
  151.         case WM_LBUTTONDBLCLK:
  152.             if (NULL==ppg->m_pPageCur)
  153.                 break;
  154.  
  155.             fDirty=ppg->m_pPageCur->OnLeftDoubleClick(wParam, LOWORD(lParam)
  156.                 , HIWORD(lParam));
  157.             break;
  158.  
  159.         case WM_MOUSEMOVE:
  160.             if (NULL==ppg->m_pPageCur)
  161.                 break;
  162.  
  163.             ppg->m_pPageCur->OnMouseMove(wParam, LOWORD(lParam)
  164.                 , HIWORD(lParam));
  165.             break;
  166.  
  167.         case WM_NCHITTEST:
  168.             if (NULL!=ppg->m_pPageCur)
  169.                 {
  170.                 //This just saves information in the page for OnSetCursor
  171.                 ppg->m_pPageCur->OnNCHitTest(LOWORD(lParam)
  172.                     , HIWORD(lParam));
  173.                 }
  174.  
  175.             return DefWindowProc(hWnd, iMsg, wParam, lParam);
  176.  
  177.         case WM_SETCURSOR:
  178.             if (NULL!=ppg->m_pPageCur)
  179.                 {
  180.                 if (ppg->m_pPageCur->OnSetCursor(LOWORD(lParam)))
  181.                     break;
  182.                 }
  183.  
  184.             return DefWindowProc(hWnd, iMsg, wParam, lParam);
  185.  
  186.         default:
  187.             return DefWindowProc(hWnd, iMsg, wParam, lParam);
  188.         }
  189.  
  190.     ppg->m_fDirty |= fDirty;
  191.     return 0L;
  192.     }
  193.  
  194.  
  195.  
  196.  
  197.  
  198. /*
  199.  * RectConvertMappings
  200.  *
  201.  * Purpose:
  202.  *  Converts the contents of a rectangle from device to logical
  203.  *  coordinates where the hDC defines the logical coordinates.
  204.  *
  205.  * Parameters:
  206.  *  pRect           LPRECT containing the rectangle to convert.
  207.  *  hDC             HDC describing the logical coordinate system.
  208.  *                  if NULL, uses a screen DC in MM_LOMETRIC.
  209.  *  fToDevice       BOOL TRUE to convert from uConv to device,
  210.  *                  FALSE to convert device to uConv.
  211.  *
  212.  * Return Value:
  213.  *  None
  214.  */
  215.  
  216. void RectConvertMappings(LPRECT pRect, HDC hDC, BOOL fToDevice)
  217.     {
  218.     POINT   rgpt[2];
  219.     BOOL    fSysDC=FALSE;
  220.  
  221.     if (NULL==pRect)
  222.         return;
  223.  
  224.     rgpt[0].x=pRect->left;
  225.     rgpt[0].y=pRect->top;
  226.     rgpt[1].x=pRect->right;
  227.     rgpt[1].y=pRect->bottom;
  228.  
  229.     if (NULL==hDC)
  230.         {
  231.         hDC=GetDC(NULL);
  232.         SetMapMode(hDC, MM_LOMETRIC);
  233.         fSysDC=TRUE;
  234.         }
  235.  
  236.     if (fToDevice)
  237.         LPtoDP(hDC, rgpt, 2);
  238.     else
  239.         DPtoLP(hDC, rgpt, 2);
  240.  
  241.     if (fSysDC)
  242.         ReleaseDC(NULL, hDC);
  243.  
  244.     pRect->left=rgpt[0].x;
  245.     pRect->top=rgpt[0].y;
  246.     pRect->right=rgpt[1].x;
  247.     pRect->bottom=rgpt[1].y;
  248.  
  249.     return;
  250.     }
  251.  
  252.  
  253.  
  254.  
  255.  
  256.  
  257.  
  258. /*
  259.  * CPages::Draw
  260.  *
  261.  * Purpose:
  262.  *  Paints the current page in the pages window.
  263.  *
  264.  * Parameters:
  265.  *  hDC             HDC to draw on, could be a metafile or printer DC or
  266.  *                  any other type of DC.
  267.  *  fNoColor        BOOL indicating if we should use screen colors or
  268.  *                  printer colos (B&W).  Objects are printed as-is, however.
  269.  *                  This is TRUE for printer DCs or print preview.
  270.  *  fPrinter        BOOL indicating if this is a printer DC in which case
  271.  *                  we eliminate some of the fancy drawing, like shadows on
  272.  *                  the page and so forth.
  273.  *
  274.  * Return Value:
  275.  *  None
  276.  */
  277.  
  278. void CPages::Draw(HDC hDC, BOOL fNoColor, BOOL fPrinter)
  279.     {
  280.     RECT            rc, rcT;
  281.     UINT            uMM;
  282.     HPEN            hPen;
  283.     HBRUSH          hBrush;
  284.     HGDIOBJ         hObj1, hObj2;
  285.     COLORREF        cr;
  286.     char            szTemp[20];
  287.     UINT            cch;
  288.     DWORD           dwExt;
  289.     LPPAGE          pPage;
  290.     RECT            rcPos;
  291.  
  292.     //Make sure the DC is in LOMETRIC
  293.     uMM=SetMapMode(hDC, MM_LOMETRIC);
  294.  
  295.     if (!fPrinter)
  296.         {
  297.         /*
  298.          * We maintain a 6mm border around the page on the screen besides
  299.          * 12.7mm margins.  We also have to account for the scroll position
  300.          * with m_*Pos which are in pixels so we have to convert them.
  301.          */
  302.         SetRect(&rcPos, m_xPos,